#import "YQPCouponCell.h"

#import "MYShopCouponView.h"
#define BGVIEW_H 400

@interface MYShopCouponView ()
@property (nonatomic, strong) UIView * bgView;
@property (nonatomic, strong) MYShopDetailsSpecificationsSelectBottonBtn * bottonView;
@property (nonatomic, strong) UILabel * titleLb;
@property (nonatomic, strong) NSMutableArray * titleArr;

@property (strong, nonatomic) YQPCouponModel *selectModel;
@end

@implementation MYShopCouponView

static NSString *couponCell = @"YQPCouponCell";


+ (void)showShopCouponViewWithModel:(id)model resultBlack
:(CouponSelectResultBlack)resultBlack{
    MYShopCouponView *screenView = [[MYShopCouponView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    screenView.backgroundColor = HexColorA(0x000000, 0.2);//HexColor(0xF3F5F9);
    screenView.model = model;
    screenView.couponBlock = resultBlack;
    [screenView showSelctTableView];
}


-(void)showSelctTableView {
    //1. 获取当前应用的主窗口
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    // 浮现动画
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.top = (kYQPScreenHeight - BGVIEW_H)-kHeight_Botton_SafeArea;
    }];
    
}

-(void)cancelSelctTableView {
    
    // 关闭动画
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.top = kYQPScreenHeight + BGVIEW_H;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma makr -- lazy
-(UIView *)bgView {
    if (!_bgView) {
        self.titleArr = NSMutableArray.new;
       
        [self.titleArr addObjectsFromArray:self.model.couponList];
        
        [self.selectTableView reloadData];
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kYQPScreenHeight+BGVIEW_H, kYQPScreenWidth, BGVIEW_H)];
        _bgView.backgroundColor = UIColor.whiteColor;
        [UIView roundedSetView:_bgView cornerRadius:12 Type:(kRoundedSetViewTypeTRL)];
        [self addSubview:_bgView];
        
        [_bgView addSubview:self.bottonView];
        [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(49);
        }];
        _titleLb = [UILabel new];
        _titleLb.textColor = HexColor(0x484848);
        _titleLb.font = kSystemMedium(18);
        _titleLb.text = kYQLS(@"优惠券使用");
        [_bgView addSubview:self.titleLb];
        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(24);
            make.height.mas_equalTo(24);
        }];
        self.selectTableView.separatorColor = HexColor(0xF4F4F4);

        [self.selectTableView registerClass:YQPCouponCell.class forCellReuseIdentifier:couponCell];
        [_bgView addSubview:self.selectTableView];
        [self.selectTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLb.mas_bottom).mas_equalTo(18);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(self.bottonView.mas_top).mas_equalTo(0);
        }];
    }
    return _bgView;
}

-(MYShopDetailsSpecificationsSelectBottonBtn *)bottonView {
    if (!_bottonView) {
        _bottonView = [MYShopDetailsSpecificationsSelectBottonBtn viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        [_bottonView.determineBtn setTitle:kYQLS(@"完成") forState:UIControlStateNormal];
        ws(self);
        [_bottonView.determineBtn addTapAction:^(id  _Nonnull sender) {
            [weakself cancelSelctTableView];
            if (weakself.selectModel) {
                if (weakself.couponBlock) {
                    weakself.couponBlock(weakself.selectModel);
                }
            }
            
        }];
    }
    return _bottonView;
}
#pragma makr -- lazy end

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPCouponModel *model = self.titleArr[indexPath.row];
    YQPCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:couponCell];
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 122;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPCouponCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    YQPCouponModel *model = self.titleArr[indexPath.row];
    model.isSelect = !model.isSelect;
    self.selectModel = model;

    cell.model = model;
    if (model.isSelect) {
        for (YQPCouponModel *Model in self.titleArr) {
            if (![model.couponHistoryId isEqualToString:Model.couponHistoryId]) {
                Model.isSelect = !model.isSelect;
            }
        }
    }
    [self.selectTableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
